﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 房主设置房主功能
	/// </summary>
	public class Action30086 : BaseAction
	{
		int tableId;
		int userId;
		int maxMultiple;
		bool playerApplyChipControl;
		bool pause;
		private bool result = false;
		public Action30086(HttpGet actionGetter)
			: base((short)ActionType.DZResponseRoomMasterFunction, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("UserId", ref userId) && 
				httpGet.GetInt("MaxMultiple", ref maxMultiple) &&
				httpGet.GetBool("PlayerApplyChipControl", ref playerApplyChipControl) &&
				httpGet.GetBool("Pause", ref pause))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var _currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息30086获得房间不存在");
				return false;
			}
			var _currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30086玩家不存在");
				return false;
			}
			if (maxMultiple < 1 && maxMultiple > 9)
			{
				Tool.WriteErrorLine("消息30086最大的控制带入筹码倍数不对");
				return false;
			}

			_currentTable.Pause = pause;
			//暂停取消 则开始游戏
			if (!pause && _currentTable.State == TableState.Waiting) //并且当前房间是等待状态
			{
				GameTable.Current.CheckStart(_currentTable);
			}
			_currentTable.PlayerApplyChipControlSwitch = playerApplyChipControl;
			if(playerApplyChipControl)
			{
				_currentTable.MaxMultiple = maxMultiple;  //只有当控制玩家带入是开着的才保存最大申请筹码的倍数
			}
			result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.tableId);
			this.PushIntoStack(this.playerApplyChipControl);
		}
	}
}
